home *** CD-ROM | disk | FTP | other *** search
/ MacAddict 104 / MacAddict_104_2005-04.iso / Software / Internet & Communication / WordPress 1.2.2 freeware.dmg / wordpress / wp-includes / wp-db.php < prev    next >
Encoding:
PHP Script  |  2004-05-01  |  7.7 KB  |  268 lines

  1. <?php
  2. //  WordPress DB Class
  3.  
  4. //  ORIGINAL CODE FROM:
  5. //  Justin Vincent (justin@visunet.ie)
  6. //    http://php.justinvincent.com
  7.  
  8. define('EZSQL_VERSION', 'WP1.25');
  9. define('OBJECT', 'OBJECT', true);
  10. define('ARRAY_A', 'ARRAY_A', false);
  11. define('ARRAY_N', 'ARRAY_N', false);
  12. if (!defined('SAVEQUERIES'))
  13.     define('SAVEQUERIES', false);
  14.  
  15. class wpdb {
  16.  
  17.     var $show_errors = true;
  18.     var $num_queries = 0;    
  19.     var $last_query;
  20.     var $col_info;
  21.  
  22.     // ==================================================================
  23.     //    DB Constructor - connects to the server and selects a database
  24.  
  25.     function wpdb($dbuser, $dbpassword, $dbname, $dbhost) {
  26.         $this->dbh = @mysql_connect($dbhost,$dbuser,$dbpassword);
  27.         if (!$this->dbh) {
  28.             die("<div>
  29.             <p><strong>Error establishing a database connection!</strong> This probably means that the connection information in your <code>wp-config.php</code> file is incorrect. Double check it and try again.</p>
  30.             <ul>
  31.             <li>Are you sure you have the correct user/password?</li>
  32.             <li>Are you sure that you have typed the correct hostname?</li>
  33.             <li>Are you sure that the database server is running?</li>
  34.             </ul>
  35.             <p><a href='http://wordpress.org/support/'>WordPress Support Forums</a></p>
  36.             </div>");
  37.         }
  38.  
  39.         $this->select($dbname);
  40.     }
  41.  
  42.     // ==================================================================
  43.     //    Select a DB (if another one needs to be selected)
  44.  
  45.     function select($db) {
  46.         if (!@mysql_select_db($db,$this->dbh)) {
  47.             die("
  48.             <p>We're having a little trouble selecting the proper database for WordPress.</p>
  49.             <ul>
  50.             <li>Are you sure it exists?</li>
  51.             <li>Your database name is currently specified as <code>" . DB_NAME ."</code>. Is this correct?</li>
  52.             <li>On some systems the name of your database is prefixed with your username, so it would be like username_wordpress. Could that be the problem?</li>
  53.             </ul>
  54.             <p><a href='http://wordpress.org/support/'>WordPress Support Forums</a></p>");
  55.         }
  56.     }
  57.  
  58.     // ====================================================================
  59.     //    Format a string correctly for safe insert under all PHP conditions
  60.     
  61.     function escape($str) {
  62.         return mysql_escape_string(stripslashes($str));                
  63.     }
  64.  
  65.     // ==================================================================
  66.     //    Print SQL/DB error.
  67.  
  68.     function print_error($str = '') {
  69.         global $EZSQL_ERROR;
  70.         if (!$str) $str = mysql_error();
  71.         $EZSQL_ERROR[] = 
  72.         array ('query' => $this->last_query, 'error_str' => $str);
  73.  
  74.         // Is error output turned on or not..
  75.         if ( $this->show_errors ) {
  76.             // If there is an error then take note of it
  77.             print "<div id='error'>
  78.             <p><strong>Database error:</strong> [$str]<br />
  79.             <code>$this->last_query</code></p>
  80.             </div>";
  81.         } else {
  82.             return false;    
  83.         }
  84.     }
  85.  
  86.     // ==================================================================
  87.     //    Turn error handling on or off..
  88.  
  89.     function show_errors() {
  90.         $this->show_errors = true;
  91.     }
  92.     
  93.     function hide_errors() {
  94.         $this->show_errors = false;
  95.     }
  96.  
  97.     // ==================================================================
  98.     //    Kill cached query results
  99.  
  100.     function flush() {
  101.         $this->last_result = null;
  102.         $this->col_info = null;
  103.         $this->last_query = null;
  104.     }
  105.  
  106.     // ==================================================================
  107.     //    Basic Query    - see docs for more detail
  108.  
  109.     function query($query) {
  110.         // initialise return
  111.         $return_val = 0;
  112.         $this->flush();
  113.  
  114.         // Log how the function was called
  115.         $this->func_call = "\$db->query(\"$query\")";
  116.  
  117.         // Keep track of the last query for debug..
  118.         $this->last_query = $query;
  119.  
  120.         // Perform the query via std mysql_query function..
  121.         $this->result = @mysql_query($query,$this->dbh);
  122.         ++$this->num_queries;
  123.         if (SAVEQUERIES) {
  124.             $this->savedqueries[] = $query;
  125.         }
  126.  
  127.         // If there is an error then take note of it..
  128.         if ( mysql_error() ) {
  129.             $this->print_error();
  130.             return false;
  131.         }
  132.  
  133.         if ( preg_match("/^\\s*(insert|delete|update|replace) /i",$query) ) {
  134.             $this->rows_affected = mysql_affected_rows();
  135.             // Take note of the insert_id
  136.             if ( preg_match("/^\\s*(insert|replace) /i",$query) ) {
  137.                 $this->insert_id = mysql_insert_id($this->dbh);    
  138.             }
  139.             // Return number of rows affected
  140.             $return_val = $this->rows_affected;
  141.         } else {
  142.             $i = 0;
  143.             while ($i < @mysql_num_fields($this->result)) {
  144.                 $this->col_info[$i] = @mysql_fetch_field($this->result);
  145.                 $i++;
  146.             }
  147.             $num_rows = 0;
  148.             while ( $row = @mysql_fetch_object($this->result) ) {
  149.                 $this->last_result[$num_rows] = $row;
  150.                 $num_rows++;
  151.             }
  152.  
  153.             @mysql_free_result($this->result);
  154.  
  155.             // Log number of rows the query returned
  156.             $this->num_rows = $num_rows;
  157.             
  158.             // Return number of rows selected
  159.             $return_val = $this->num_rows;
  160.         }
  161.  
  162.         return $return_val;
  163.     }
  164.  
  165.     // ==================================================================
  166.     //    Get one variable from the DB - see docs for more detail
  167.  
  168.     function get_var($query=null, $x = 0, $y = 0) {
  169.         $this->func_call = "\$db->get_var(\"$query\",$x,$y)";
  170.         if ( $query )
  171.             $this->query($query);
  172.  
  173.         // Extract var out of cached results based x,y vals
  174.         if ( $this->last_result[$y] ) {
  175.             $values = array_values(get_object_vars($this->last_result[$y]));
  176.         }
  177.  
  178.         // If there is a value return it else return null
  179.         return (isset($values[$x]) && $values[$x]!=='') ? $values[$x] : null;
  180.     }
  181.  
  182.     // ==================================================================
  183.     //    Get one row from the DB - see docs for more detail
  184.  
  185.     function get_row($query = null, $output = OBJECT, $y = 0) {
  186.         $this->func_call = "\$db->get_row(\"$query\",$output,$y)";
  187.         if ( $query )
  188.             $this->query($query);
  189.  
  190.         if ( $output == OBJECT ) {
  191.             return $this->last_result[$y] ? $this->last_result[$y] : null;
  192.         } elseif ( $output == ARRAY_A ) {
  193.             return $this->last_result[$y] ? get_object_vars($this->last_result[$y]) : null;
  194.         } elseif ( $output == ARRAY_N ) {
  195.             return $this->last_result[$y] ? array_values(get_object_vars($this->last_result[$y])) : null;
  196.         } else {
  197.             $this->print_error(" \$db->get_row(string query, output type, int offset) -- Output type must be one of: OBJECT, ARRAY_A, ARRAY_N");
  198.         }
  199.     }
  200.  
  201.     // ==================================================================
  202.     //    Function to get 1 column from the cached result set based in X index
  203.     // se docs for usage and info
  204.  
  205.     function get_col($query = null , $x = 0) {
  206.         if ( $query )
  207.             $this->query($query);
  208.  
  209.         // Extract the column values
  210.         for ( $i=0; $i < count($this->last_result); $i++ ) {
  211.             $new_array[$i] = $this->get_var(null, $x, $i);
  212.         }
  213.         return $new_array;
  214.     }
  215.  
  216.     // ==================================================================
  217.     // Return the the query as a result set - see docs for more details
  218.  
  219.     function get_results($query = null, $output = OBJECT) {
  220.         $this->func_call = "\$db->get_results(\"$query\", $output)";
  221.  
  222.         if ( $query )
  223.             $this->query($query);
  224.  
  225.         // Send back array of objects. Each row is an object
  226.         if ( $output == OBJECT ) {
  227.             return $this->last_result;
  228.         } elseif ( $output == ARRAY_A || $output == ARRAY_N ) {
  229.             if ( $this->last_result ) {
  230.                 $i = 0;
  231.                 foreach( $this->last_result as $row ) {
  232.                     $new_array[$i] = (array) $row;
  233.                     if ( $output == ARRAY_N ) {
  234.                         $new_array[$i] = array_values($new_array[$i]);
  235.                     }
  236.                     $i++;
  237.                 }
  238.                 return $new_array;
  239.             } else {
  240.                 return null;
  241.             }
  242.         }
  243.     }
  244.  
  245.  
  246.     // ==================================================================
  247.     // Function to get column meta data info pertaining to the last query
  248.     // see docs for more info and usage
  249.  
  250.     function get_col_info($info_type = 'name', $col_offset = -1) {
  251.         if ( $this->col_info ) {
  252.             if ( $col_offset == -1 ) {
  253.                 $i = 0;
  254.                 foreach($this->col_info as $col ) {
  255.                     $new_array[$i] = $col->{$info_type};
  256.                     $i++;
  257.                 }
  258.                 return $new_array;
  259.             } else {
  260.                 return $this->col_info[$col_offset]->{$info_type};
  261.             }
  262.         }
  263.     }
  264. }
  265.  
  266. $wpdb = new wpdb(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST);
  267.  
  268. ?>